<?php
// 引入工具类
require_once './util/DButil.php';
// 获取请求action参数
$action = $_GET['action']?$_GET['action']:'';
// 返回前端的数据封装
$result = array();

if ($action == 'getlist'){
    session_start();
    // 获取用户类型
    $u_type = $_SESSION['userinfo']['u_type'];
    $u_id = $_SESSION['userinfo']['u_id'];

    $sql = "select o.o_id , u.u_name,t.t_type,t.recycle_price,u.u_phone,o.o_address,o.o_time,o.imege,o.state, o.finish_time,o.score,o.estimate,o.create_time,o.update_time
from treash t , userinfo u ,  orders o where t.t_id=o.t_id and u.u_id=o.u_id";
    $parameter = array();
    // 普通用户只查自己的
    if($u_type =='1'){
        $sql .= "  and u.u_id = ?";
        $parameter[] = $u_id;
    }
//    var_dump($sql);
//    var_dump($parameter);
    $list = selectList($sql,$parameter);
    echo json_encode($list);
}elseif ($action == 'state'){
    $state = $_GET['state'];
    $o_id = $_GET['o_id'];
    $sql = "update orders set state=? ,update_time=now() where o_id=?";
    echo insAndDelAndUpd($sql,array($state,$o_id));
}elseif ($action == 'estimate'){
    $obj = json_decode(file_get_contents("php://input"),true);
    $score = $obj['score'];
    $estimate = $obj['estimate'];
    $o_id = $obj['o_id'];
    $sql = "update orders set score=?, estimate=?,  update_time=now() where o_id=?";
    echo insAndDelAndUpd($sql,array($score,$estimate,$o_id));
}elseif ($action == 'add'){
    session_start();
    $u_id = $_SESSION['userinfo']['u_id'];
    $obj = json_decode(file_get_contents("php://input"),true);
    $t_id = $obj['t_id'];
    $o_address = $obj['o_address'];
    $o_time = $obj['o_time'];
    $imege = $obj['imege'];
    $sql = "insert into orders values(null,?,?,?,?,?,'0',null,null,null,default ,now())";
    echo insAndDelAndUpd($sql,array($t_id,$u_id,$o_address,$o_time,$imege));
}else{
    echo '不要导弹';
}